<?php
/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/9/4
 * Time: 19:27
 */

namespace app\common\util;


class Assert
{
    public static function notEmpty($data, $fields)
    {
        $rt = [];
        foreach ($fields as $f) {
            if (empty($data[$f])) {
                $rt = json_encode([
                    'status' => 0,
                    'info' => '参数错误'
                ]);
                if (!empty($_GET['callback'])) {
                    $rt = $_GET['callback'] . '(' . $rt . ')';
                }
                exit($rt);
            }
            $rt[$f] = trim($data[$f]);
        }
        return $rt;
    }

    public static function isEmail($email_address)
    {
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        if (preg_match($pattern, $email_address)) {
            return true;
        }
        return false;
    }

    public static function isIntValArray($data)
    {
        if (empty($data) || !is_array($data)) {
            exit(json_decode(['status' => 0, 'info' => 'error']));
        }
        foreach ($data as $value) {
            if (!is_numeric($value)) {
                exit(json_decode(['status' => 0, 'info' => 'error']));
            }
        }
    }
}